package io.kiki.sba.registry.common.model.metaserver.nodes;

import io.kiki.sba.registry.common.model.Node;
import io.kiki.sba.registry.common.model.store.Url;
import lombok.Getter;
import lombok.NonNull;

import java.util.Objects;

public abstract class AbstractNode implements Node {
    protected final Url url;
    @Getter
    protected final String regionId;
    @Getter
    protected final String dataCenter;
    @Getter
    protected final String ip;

    protected AbstractNode(@NonNull String dataCenter, @NonNull Url url, String regionId) {

        this.dataCenter = dataCenter;
        this.url = url;
        this.regionId = regionId;
        this.ip = getIp();
    }

    public String getIp() {
        return url.getIp();
    }

    public Url getUrl() {
        return url;
    }

    public String getAddressString() {
        return url.buildAddressString();
    }

    protected boolean equal(AbstractNode node) {
        return Objects.equals(ip, node.ip) && Objects.equals(regionId, node.regionId) && Objects.equals(dataCenter, node.dataCenter) && Objects.equals(getAddressString(), node.getAddressString());
    }

    protected int hash() {
        return Objects.hash(ip, regionId, dataCenter, getAddressString());
    }


}
